<?php

namespace IDock\Insurance\ZSIC\Response;

use IDock\Http\Response;
use IDock\IDock\IDockEnums;
use IDock\Insurance\ZSIC\Response\AllZSICResponse;
use IDock\Insurance\ZSIC\ZSICEnums;
use IDock\Printer\Premium\PremiumInquiryPrinter;
use IDock\Printer\ValidateCarPrinter;
use IDock\Printer\VehicleModelPrinter;
use IDock\Printer\VehiclePrinter;


class VehicleCheckResponse extends AllZSICResponse
{
    /**
     * @var ValidateCarPrinter $printer
     * @var  $printer
     */
    public $printer = ValidateCarPrinter::class;

    public function resolve(): Response
    {
        $body = $this->getBody();
        if ($this->printer) {
            $this->printer = new $this->printer;
        }
        if ($body) {
            $body = str_replace('&','&amp;',$body);
            $xml_parser = xml_parser_create();
            if(!xml_parse($xml_parser,$body,true)){
                xml_parser_free($xml_parser);
                $this->setError(  '保险公司返回格式异常');
                return  $this;
            }
            $xml = simplexml_load_string($body);
            $body = $this->objectToArray($xml);
            $errorMsg = $body['head']['resultCode'] ?? '';
            $message = $body['head']['errorMsg'] ?? '';
            $this->setBody($body);
            if ($errorMsg != '1') {
                $this->setError(  ($errorMsg ?: '') .":".$message);
                return  $this;
            }
        }

        return $this;
    }

    public function printerInit(): Response
    {
        $body = $this->getBody();
        $this->printer->url = $body['body']['vehicleInspection']['cInspectCode'] ?? '';
        return $this;
    }

}
